<?php
namespace app\agent\logic;

use app\common\model\WkSellerexModel;
use think\Db;
use think\Exception;

class WkSellerexLogic extends WkSellerexModel
{

	public function getPage($where = [], $request = [])
	{
	    $field = [
	        'tginfo.truename', 'tginfo.tgnumber',
            'tguser.telephone tg_telephone',
            'user.telephone',
            'seller.user_id', 'seller.user_id', 'seller.applyTime',
            'info.checkTime',
            'sellerex.uid', 'sellerex.shopname', 'sellerex.address', 'sellerex.id',
        ];

	    $join = [
            ['wk_seller seller', 'seller.uid = sellerex.uid'],
            ['wk_sellerinfo info', 'info.uid = sellerex.uid'],
            ['user_tginfo tginfo', 'tginfo.uid = seller.bind_uid'],
            ['user tguser', 'tguser.uid = tginfo.uid'],
            ['user', 'user.uid = sellerex.uid'],
        ];

		//排序
		$order = [
				$this->getPk() => 'DESC',
		];

		//查询数据
		$data = $this->alias('sellerex')->field($field)->join($join)->where($where)->order($order)->paginate(10, false, ['query' => $request]);

		//字段属性映射
		foreach ($data as $key => &$val)
		{
			$this->attributeMap($val);
		}

		return $data;
	}

	public function updateRecord($data = [])
	{
		//入库
		$data = $this->allowField(true)->save($data, [$this->getPk() => $data['id']]);

		if( $data === false )
		{
			return false;
		}

		return true;
	}

	public function destroyRecord($id = 0)
	{
		return $this->delete($id);
	}

	public function getDetail($where = [])
	{
		//查询数据
		$data = $this->alias('sellerex')->where($where)->find();

		//字段属性映射
		$data = $this->attributeMap($data);

		return $data;
	}

    /**
     * 获取代理商辖区内的所有商家
     * @param $type int 代理商级别
     * @param $addr_code int 辖区
     * @return float|string
     */
    public function getSellerexByType($type, $addr_code)
    {
        $category = $this->_map['type'][$type];

        $where = [
            ['sellerex.' . $category, '=', $addr_code],
        ];

        return $this->alias('sellerex')->where($where)->count('sellerex.uid');
    }


    public function getExcel($where = [], $request = [])
    {
        $field = [
            'tginfo.truename', 'tginfo.tgnumber',
            'user.telephone',
            'seller.user_id', 'seller.user_id', 'seller.applyTime',
            'info.checkTime',
            'sellerex.uid', 'sellerex.shopname', 'sellerex.tel', 'sellerex.address', 'sellerex.id',
        ];

        $join = [
            ['wk_seller seller', 'seller.uid = sellerex.uid'],
            ['wk_sellerinfo info', 'info.uid = sellerex.uid'],
            ['user_tginfo tginfo', 'tginfo.uid = seller.bind_uid'],
            ['user', 'user.uid = tginfo.uid'],
        ];

        //排序
        $order = [
            $this->getPk() => 'DESC',
        ];

        //查询数据
        $data = $this->alias('sellerex')->field($field)->join($join)->where($where)->order($order)->select()->toArray();

        //字段属性映射
        foreach ($data as $key => &$val)
        {
            $this->attributeMap($val);
        }

        return $data;
    }

}